# -*- coding: utf-8 -*-
# @Time : 2024/1/24 2:23 PM 
# @Author : Wayne
# @Email : 1
# @File : group1.py
# @Project : young_python_demo
# @DESCRIPTION :

"""
data_list 需要分组的字典数组
fields 需要分组的字段
"""
def group_by_fields(data_list, *fields):
    from collections import defaultdict
    from operator import itemgetter
    result_group = defaultdict(list)
    for item in data_list:
        key = itemgetter(*fields)(item)
        key_str = '-'.join(map(str, key))
        result_group[key_str].append(item)
    return dict(result_group).values()

'''参考代码执行区'''
data_list = [
    {
        "sumOrderNumber":"222",
        "order":"s33443",
    },
    {
        "sumOrderNumber": "65656",
        "order": "4dfd",
    },
    {
        "sumOrderNumber": "222",
        "order": "545454",
    }
]
new_data_list = group_by_fields(data_list, "sumOrderNumber")
# 返回结果分组成 2 个数组
# dict_values([[{'sumOrderNumber': '222', 'order': 's33443'}, {'sumOrderNumber': '222', 'order': '545454'}], [{'sumOrderNumber': '65656', 'order': '4dfd'}]])
